package org.lpw.wormhole.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lpw.wormhole.WormholeManager;
import org.lpw.wormhole.element.Element;
import org.lpw.wormhole.queue.LocalQueue;
import org.lpw.wormhole.util.Kit;

/**
 * 远程队列Servlet服务。用于处理基于Servlet的远程队列同步。
 * 
 * @author lpw
 */
public class RemoteQueueServlet
    extends HttpServlet
{
    private static final long serialVersionUID = 4310241644348447898L;
    private static final String ELEMENT = "element";

    protected final Kit kit;
    protected final LocalQueue localQueue;

    public RemoteQueueServlet()
    {
        super();

        kit = WormholeManager.getInstance().getKit();
        localQueue = WormholeManager.getInstance().getQueueFactory().getLocalQueue();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        String string = request.getParameter(ELEMENT);

        if(string==null)
            return;

        Element element = kit.getConverter().deserializeFromHexString(string);

        if(element!=null)
            localQueue.push(element);
    }
}
